﻿using System;
using System.Windows.Forms;

using Moe.ECamel.Common.Entities;
using Moe.ECamel.Client.Controllers;
using Moe.ECamel.Client.WinForms.Common;
using Moe.ECamel.Client.WinForms.Forms;
using Moe.ECamel.Common.Security;
using Moe.ECamel.Common.Utils;

namespace Moe.ECamel.Client.WinForms.Controls
{
	public partial class SubscriptionListControl : ECamelUserControl
	{
		private SubscriptionsController _controller;

		public SubscriptionListControl()
		{
			InitializeComponent();
		}

		public event EventHandler SubscriptionAdded;

		public Subscription CurrentEntity
		{
			get { return (Subscription)ListBindingSource.Current; }
		}

		public void Initialize(SubscriptionsController controller)
		{
			this._controller = controller;
			this.ListBindingSource.DataSource = _controller.List;
			ApplyPermissions();
			LoadSubscriptionTypes();
		}

		private void ApplyPermissions()
		{
			EntityAddToolStripDropDownButton.Available = EntityAddToolStripMenuItem.Available = ECamelHelper.IsPermitted(Operations.SubscriptionAdd);
			UpdateSubscriptionStatusToolStripButton.Available = UpdateSubscriptionStatusToolStripMenuItem.Available = ECamelHelper.IsPermitted(Operations.SubscriptionEdit);
			RenewToolStripButton.Available = RenewToolStripMenuItem.Available = ECamelHelper.IsPermitted(Operations.SubscriptionAdd);
			EntityViewToolStripButton.Available = EntityViewToolStripMenuItem.Available = ECamelHelper.IsPermitted(Operations.SubscriptionView);
		}

		private void LoadSubscriptionTypes()
		{
			if (!EntityAddToolStripDropDownButton.Available)
				return;

			foreach (var subscriptionType in ECamelHelper.SubscriptionTypes.Values)
			{
				if (!subscriptionType.Active)
					continue;
				var item = new ToolStripButton("מנוי " + subscriptionType.SubscriptionTypeName);
				item.Tag = subscriptionType;
				item.Width = 60;
				item.Click += EntityAddToolStripButton_Click;
				EntityAddToolStripDropDownButton.DropDownItems.Add(item);
			}
		}
		public void RefreshView()
		{
			// Refresh entity list:
			this._controller.RefreshList();
			// Refresh binding:
			this.ListBindingSource.CurrencyManager.Refresh();
			// Refresh menus:
			RefreshMenus();
		}
		private void RefreshMenus()
		{
			// Get current entity:
			var entity = ListBindingSource.Current;
			// Refresh available controls:
			bool hasValue = (entity != null);
			EntityViewToolStripButton.Enabled = EntityViewToolStripMenuItem.Enabled = hasValue;
			UpdateSubscriptionStatusToolStripButton.Enabled = UpdateSubscriptionStatusToolStripMenuItem.Enabled = hasValue;
		}

		private void AddEntity(SubscriptionType subscriptionType)
		{
			var subscription = _controller.NewSubscription(subscriptionType);
			using (var form = new SubscriptionDetailsForm(_controller.NewCreateController(subscription)))
			{
				DialogResult result = form.ShowDialog();
				if (result == DialogResult.OK)
				{
					RefreshView();
					RaiseSubscriptionAdded();
				}
			}
		}
		private void ViewEntity()
		{
			// Get current entity:
			var entity = (Subscription)ListBindingSource.Current;
			if (entity != null)
			{
				using (var form = new SubscriptionDetailsForm(_controller.NewViewController(entity)))
				{
					form.ShowDialog();
				}
			}
		}
		private void UpdateSubscriptionStatus()
		{
			// Get current entity:
			var entity = (Subscription)ListBindingSource.Current;
			if (entity == null)
				return;
			var error = _controller.UpdateSubscriptionStatus(entity, !entity.Active);
			if (error != null)
				ECamelHelper.ShowMessage(error);
			else
				RefreshView();
		}
		private void RenewSubscription()
		{
			// Get current entity:
			var entity = (Subscription)ListBindingSource.Current;
			if (entity == null)
				return;
			var error = _controller.Renew(entity, SystemContainer.Clock.Today);
			if (error != null)
				ECamelHelper.ShowMessage(error);
			else
				RefreshView();
		}

		private void EntityAddToolStripButton_Click(object sender, EventArgs e)
		{
			var item = (ToolStripItem)sender;
			var subscriptionType = (SubscriptionType)item.Tag;
			AddEntity(subscriptionType);
		}
		private void EntityViewToolStripButton_Click(object sender, EventArgs e)
		{
			ViewEntity();
		}
		private void UpdateSubscriptionStatusToolStripButton_Click(object sender, EventArgs e)
		{
			UpdateSubscriptionStatus();
		}
		private void RenewToolStripButton_Click(object sender, EventArgs e)
		{
			RenewSubscription();
		}

		private void EntityViewToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ViewEntity();
		}
		private void EntityAddToolStripMenuItem_Click(object sender, EventArgs e)
		{
			var control = (Control)sender;
			var subscriptionType = (SubscriptionType)control.Tag;
			AddEntity(subscriptionType);
		}
		private void UpdateSubscriptionStatusToolStripMenuItem_Click(object sender, EventArgs e)
		{
			UpdateSubscriptionStatus();
		}
		private void RenewToolStripMenuItem_Click(object sender, EventArgs e)
		{
			RenewSubscription();
		}

		private void ListDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			ViewEntity();
		}

		private void RaiseSubscriptionAdded()
		{
			if (this.SubscriptionAdded != null)
				this.SubscriptionAdded(this, EventArgs.Empty);
		}
	}
}